/*
 Copyright (c) 2008 TPSi
 Alex Iskander
 
 Permission is hereby granted, free of charge, to any person obtaining a copy
 of this software and associated documentation files (the "Software"), to deal
 in the Software without restriction, including without limitation the rights
 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 copies of the Software, and to permit persons to whom the Software is
 furnished to do so, subject to the following conditions:
 
 The above copyright notice and this permission notice shall be included in
 all copies or substantial portions of the Software.
 
 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 THE SOFTWARE.
 
 */

using namespace Bind;
class TestBindable : public BindableObject
{
public:
	TestBindable()
	{
		this->addProperty("number", &number);
		this->addProperty("text", &text);
	}
	
	Bound<int> number;
	Bound<std::string> text;
};


TEST(BindableTest, IsWorking)
{
	TestBindable b1;
	b1.number = 5;
	b1.text = "Hello, world";
	
	
	TestBindable b2;
	
	//set-up bindings
	b2.number.bindTo(new NameSelector("number"));
	b2.text.bindTo(new NameSelector("text"));
	
	
	b2.bind(&b1);
	
	EXPECT_EQ(5, b2.number);
	EXPECT_EQ(std::string("Hello, world"), b2.text.get());
	
	b1.number = 10;
	b1.text = "Hello, Again";
	
	EXPECT_EQ(10, b2.number);
	EXPECT_EQ(std::string("Hello, Again"), b2.text.get());
	
}

TEST(BindableTest, Bake)
{
	TestBindable b1;
	b1.number = 5;
	b1.text = "Hello, world";
	
	TestBindable b2;
	b2.number.bindTo(new NameSelector("number"));
	b2.text.bindTo(new NameSelector("text"));
	
	b2.bind (&b1);
	
	EXPECT_EQ(5, b2.number);
	EXPECT_EQ(std::string("Hello, world"), b2.text.get());
	
	b2.bake();
	
	b1.number = 10;
	b1.text = "Hello, Again";
	
	EXPECT_EQ(5, b2.number);
	EXPECT_EQ(std::string("Hello, world"), b2.text.get());
	
}

class DocumentTest : public BindableObject
{
public:
	DocumentTest()
	{
		this->addProperty("total-pages", this->totalPages);
	}
	
	Bound<int> totalPages;
};

class PageDataTest : public BindableObject
{
public:
	PageDataTest()
	{
		this->addProperty("page-number", this->pageNumber);
	}
	
	Bound<int> pageNumber;
};

class PageTest : public BindableObject
{
public:
	PageTest()
	{
		this->addProperty("data", this->data);
		this->addProperty("width", this->width);
		this->addProperty("height", this->height);
	}
		
	Bound<PageDataTest *> data;
	
	Bound<double> width, height;
};

class FrameTest : public BindableObject
{
public:
	FrameTest()
	{
		this->addProperty("x", this->x);
		this->addProperty("y", this->y);
	}
	
	Bound<double> x, y;

};

TEST(BindableTest, Complex)
{
/*
 Some portions of this test have not yet been implemented, as the elements
 which they are to test have not been implemented.
 */
	/* Prepare */
	DocumentTest *document = new DocumentTest();
	PageDataTest *pageData = new PageDataTest();
	PageTest *page = new PageTest();
	FrameTest *frame1 = new FrameTest();
	FrameTest *frame2 = new FrameTest();
	
	document->bindChild(page);
	page->bindChild(frame1);
	page->bindChild(frame2);
	
	page->width = 8.5;
	page->height = 11;
	
	page->data = pageData;
	
	
	/* Test inheritable properties */
	NameSelector *inheritSelect = new NameSelector("width");
	Bound<double> *inheritTest
		= dynamic_cast< Bound<double> *> (inheritSelect->get(frame1));
	
	ASSERT_TRUE(inheritTest);
	ASSERT_EQ(inheritTest->get(), 8.5);
	
	delete inheritSelect;
	
	
	/* Negative test non-inheritable properties */
	//oops: non-inheritable properties do not yet exist.
	
	/* Test Parent Selecting */
	//oops: no parent selector yet
	
	/* Test chaining a name selector with Parent selector */
	//again: no parent selector
	
	/* Cleanup */
	delete frame1;
	delete frame2;
	delete pageData;
	delete page;
	delete document;
	
}

